import cPickle
try:
	import MySQLdb as sqldb
except:
	import pymysql as sqldb
import sys,re
import os
from collections import Counter
from collections import defaultdict
from operator import itemgetter
import numpy as np
from numpy import arange,array,ones,linalg,bincount
conn=sqldb.connect(host='198.133.224.186',user='root',passwd='root',db="bl")


def from_no():
	fin=sys.argv[1]
	t=open(fin).readlines()
	t=[int(v.strip()) for v in t]
	d=cPickle.load(open("clus_no.db"))
	out=[]
	for k in d:
		if d[k] in t:
				out.append(k[:-1])
	for i in out:
		sql="insert into bl(gid,ctitle,ctmpl,ckws,hserver) values(%s,%s,%s,%s,%s)"
		cur=conn.cursor()
		cur.execute(sql,i)
		conn.commit()

def gen_bl():
	sql="select gid,ctitle,ctmpl,ckws,hserver from bl"
	cur=conn.cursor()
	cur.execute(sql)
	tmp=cur.fetchall()
	print len(tmp)
	tmp=list(set(tmp))
	print len(tmp)
	cur.close()
	cno_db="clus_no.db"
	db=cPickle.load(open(cno_db))
	blacklist={}
	for k in db:
		if k[:5] in tmp:
			blacklist[k]=1
			blacklist[db[k]]=1
		if not k[0] and not k[1] and not k[2] and not k[3] and not k[4]:
			blacklist[k]=1
			blacklist[db[k]]=1
	print len(blacklist)
	f=open("bl.db","w")
	cPickle.dump(blacklist,f)
	f.close()


if __name__ == '__main__':
	gen_bl()

